package matchthreegame.handlers;

import java.lang.ref.WeakReference;

import matchthreegame.dataBase.DBManagerHelper;
import matchthreegame.extensions.MatchThreeGameZoneExtension;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;

public class GetAmountOfMoneyRequestHandler  extends BaseClientRequestHandler {

	public static final String AMOUNT_OF_MONEY = "AOM";
	
	MatchThreeGameZoneExtension zoneExtension;
	
	@Override
	public void handleClientRequest(User sender, ISFSObject params) {

		zoneExtension = (MatchThreeGameZoneExtension)getParentExtension().getParentZone().getExtension();
		
		WeakReference<DBManagerHelper> dbHelper = null;
		long amountOfMoney = 0;
		
		try {
			dbHelper = new WeakReference<DBManagerHelper>(zoneExtension.getDBHelper());
			
			// Get amount of money for sender
			amountOfMoney = dbHelper.get().getAmountOfMoney(sender.getName());
			
		} catch (Exception e) {
			trace(e.getMessage().toString());
		}
		
		SFSObject sfsAmountOfMoney = new SFSObject();
		sfsAmountOfMoney.putLong(AMOUNT_OF_MONEY, amountOfMoney);
		
		zoneExtension.send(MatchThreeGameZoneExtension.CMD_GET_AMOUNT_OF_MONEY, sfsAmountOfMoney, sender);
		
	}

}
